package com.zhouyu.paydemo.unipay.qrcode.BS;

import com.zhouyu.paydemo.unipay.qrcode.DemoBase;
import com.zhouyu.paydemo.unipay.sdk.QrcService;
import com.zhouyu.paydemo.unipay.sdk.SDKConfig;
import lombok.extern.slf4j.Slf4j;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * 用途：接收c2b附加处理请求结果通知样例<br>
 * 日期： 2017-03<br>
 * 声明：以下代码只是为了方便商户测试而提供的样例代码，商户可以根据自己需要，按照技术文档编写。该代码仅供参考，不提供编码，性能，规范性等方面的保障<br>
 */
@SuppressWarnings("serial")
@Slf4j
public class C2BAddnRecevieNotify extends HttpServlet{

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		
		String acqCode = req.getParameter("acqCode");
		String qrNo = req.getParameter("qrNo");
		String respCode_form = req.getParameter("respCode");
		String voucherNum = req.getParameter("voucherNum");
		String upReserved = req.getParameter("upReserved");
		
		String couponInfo_type = req.getParameter("couponInfo_type");
		String couponInfo_spnsrId = req.getParameter("couponInfo_spnsrId");
		String couponInfo_offstAmt = req.getParameter("couponInfo_offstAmt");
		String couponInfo_id = req.getParameter("couponInfo_id");
		String couponInfo_desc = req.getParameter("couponInfo_desc");
		String txnAmt = req.getParameter("txnAmt");
		String origTxnAmt = req.getParameter("origTxnAmt");
		
		/**
		 * 组装请求报文
		 */
		Map<String, String> contentData = new HashMap<String, String>();
		contentData.put("version", "1.0.0");
		contentData.put("reqType", "0240000903");
		contentData.put("issCode", acqCode);
		
		contentData.put("qrNo", qrNo);
		contentData.put("respCode", respCode_form);//可以按照规范修改应答码
		contentData.put("respMsg", "成功[0000000]");

		contentData.put("upReserved", upReserved);
		contentData.put("voucherNum", voucherNum);
		
		if(!"".equals(couponInfo_type) && 
		   !"".equals(couponInfo_spnsrId) &&
		   !"".equals(couponInfo_offstAmt) &&
		   !"".equals(couponInfo_id) &&
		   !"".equals(couponInfo_type) &&
		   !"".equals(txnAmt) &&
		   !"".equals(origTxnAmt)){
			
			String couponInfoStr = "[{"
							+ "\"type\": \"" + couponInfo_type + "\""
							+ ", \"spnsrId\" : \"" + couponInfo_spnsrId + "\""
							+ ", \"offstAmt\" : \"" + couponInfo_offstAmt + "\""
							+ ", \"id\" : \"" + couponInfo_id + "\""
							+ ", \"desc\" : \"" + couponInfo_desc + "\""
							+ "}]"; //可替换使用第三方json库组json
			//付款方适配活动：付款方需要明确指出活动的出资方，允许以下三种情况，即付款方单独出资、付款方与银联共同出资、银联单独出资
			contentData.put("couponInfo", QrcService.base64Encode(couponInfoStr,"UTF-8"));
			contentData.put("txnAmt", txnAmt);
			contentData.put("origTxnAmt", origTxnAmt);
		}
		
		Map<String, String> reqData = QrcService.sign(contentData,DemoBase.encoding);			 //报文中certId,signature的值是在signData方法中获取并自动赋值的，只要证书配置正确即可。
		String requestUrl = SDKConfig.getConfig().getQrcB2cIssBackTransUrl();
		Map<String, String> rspData = QrcService.post(reqData,requestUrl,DemoBase.encoding);  //发送请求报文并接受同步应答（默认连接超时时间30秒，读取返回结果超时时间30秒）;这里调用signData之后，调用submitUrl之前不能对submitFromData中的键值对做任何修改，如果修改会导致验签不通过
		
		if(!rspData.isEmpty()){
			if(QrcService.validate(rspData, DemoBase.encoding)){
				log.info("验证签名成功");
				String respCode = rspData.get("respCode") ;
				if(("00").equals(respCode)){

				}else{
					//其他应答码为失败请排查原因
					//TODO
				}
			}else{
				log.error("验证签名失败");
				//TODO 检查验证签名失败的原因
			}
		}else{
			//未返回正确的http状态
			log.error("未获取到返回报文或返回http状态码非200");
		}
		String reqMessage = DemoBase.genHtmlResult(reqData);
		String rspMessage = DemoBase.genHtmlResult(rspData);
		resp.getWriter().write("</br>请求报文:<br/>"+reqMessage+"<br/>" + "应答报文:</br>"+rspMessage+"");
	}
}
